package cn.jiangjiesheng.bootstrap.fileport.interfaces.model;

import com.alibaba.fastjson.JSON;
import cn.jiangjiesheng.bootstrap.commons.exception.BizzException;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;

/**
 * Created by dev@jiangjiesheng.cn on 2021/11/4  <br/>
 *
 * @author dev@jiangjiesheng.cn
 */
public class ExcelPictModel {

    private int col;

    private int row;

    private byte[] bytes;

    /**
     * 取值接口：WorkBook PICTURE_TYPE_PNG
     */
    private int picType;

    private String picTypeString;

    private String url;

    private File file;

    @Getter
    private int height;

    @Getter
    private Integer weight;

    private ExcelPictModel(){}

    public static ExcelPictModel builder(){
        return new ExcelPictModel();
    }

    public ExcelPictModel setRow(int row){
        this.row = row;
        return this;
    }

    public ExcelPictModel setCol(int col){
        this.col = col;
        return this;
    }

    /**
     * 解析网络图片
     */
    public ExcelPictModel setUrl(String url) {
        this.url = url;
        if (StringUtils.isBlank(this.url)) {
            throw new BizzException("url file cannot be empty.");
        }
        this.picType = Workbook.PICTURE_TYPE_JPEG;
        this.picTypeString = "jpg";
        if (url.contains("png")) {
            this.picType = Workbook.PICTURE_TYPE_PNG;
            this.picTypeString = "png";
        } else if (url.contains("wmf")) {
            this.picType = Workbook.PICTURE_TYPE_WMF;
            this.picTypeString = "wmf";
        }
        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(new URL(this.url));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, picTypeString, baos);
            this.bytes = baos.toByteArray();
            this.height = bufferedImage.getHeight();
            this.weight = bufferedImage.getWidth();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }

    public ExcelPictModel build(){
        return this;
    }

    public byte[] getBytes() {
        return bytes;
    }

    public int getPicType() {
        return picType;
    }

    public int getCol() {
        return col;
    }

    public int getRow() {
        return row;
    }

    @Override
    public String toString(){
        return JSON.toJSONString(this);
    }

    public ExcelPictModel setFile(File file) {
        this.file = file;
        if (Objects.isNull(this.file)) {
            throw new BizzException("url file cannot be empty.");
        }
        this.picType = Workbook.PICTURE_TYPE_JPEG;
        this.picTypeString = "jpg";
        if (file.getPath().contains(".png")) {
            this.picType = Workbook.PICTURE_TYPE_PNG;
            this.picTypeString = "png";
        } else if (file.getPath().contains(".wmf")) {
            this.picType = Workbook.PICTURE_TYPE_WMF;
            this.picTypeString = "wmf";
        }
        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(this.file);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, picTypeString, baos);
            this.bytes = baos.toByteArray();
            this.height = bufferedImage.getHeight();
            this.weight = bufferedImage.getWidth();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }
}
